<?php
    include($_SERVER['DOCUMENT_ROOT']."/ObstacleWebApp/view/headeradmin.php");
?>

<section>
<hgroup>
    <h1>Gebouw toevoegen</h1>
</hgroup>
<?php if(!isset($_POST['Submit'])) { ?>
<form  name="registerform" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <table>
        <tr>
            <td>Straat</td>
            <td><input required placeholder="Straat" name="straat" type="text"></td>
        </tr>
        <tr>
            <td>Huisnummer</td>
            <td><input required placeholder="Huisnummer" name="huisnummer" type="text"></td>
        </tr>
        <tr>
            <td>Postcode</td>
            <td>
                <select name="postcode" required>
                <?php
                    // Gets all the ZIP-codes and cities from the databases and selects the one that corresponds with the ZIP-code of the selected building
                    $Cities = GetAllCityOrderByZIP();
                    while($cityRow = mysql_fetch_array($Cities))
                    {
                       echo "<option value=\"".$cityRow['Id']."\">".$cityRow['ZIP']." (".$cityRow['City'].")</option>"; 
                    }
                ?>
                </select>
            </td>
        </tr>
        <tr>
            <td>Bouwjaar</td>
            <td>
                <select name="bouwjaar" required>
                <?php
                for ($i = $curYear = date('Y'); $i>= 1930; $i--)
                {
                    echo "<option value=\"".$i."\">".$i."</option>";
                }
                ?>
                </select>
            </td>
        </tr>
        <tr>
            <td>Type</td>
            <td>
                <select name="type" required>
                <?php
                    // Gets all the building-types from the databases and selects the one that corresponds with the building-type of the selected building
                    $BuildingTypes = GetAllBuildingtype();
                    while($buildingtypeRow = mysql_fetch_array($BuildingTypes))
                    {
                       echo "<option value=\"".$buildingtypeRow['Id']."\">".$buildingtypeRow['Name']."</option>"; 
                    }
                ?>
                </select>
            </td>
        </tr>
        <?php

            // Gets all the building details from the databases and shows the text of the building details that are known for the selected building
            $BuildingSpecs = GetAllBuildingSpecs();
            while($buildingspecRow = mysql_fetch_array($BuildingSpecs))
            {
                echo "<tr><td>".$buildingspecRow['BuildingKey']."</td>";
                echo "<td><input placeholder=\"".$buildingspecRow['Comment']."\" name=\"".$buildingspecRow['Id']."\" type=\"text\"></td>";
            }

        ?>
 	<tr>
            <td><input type="submit" name="Submit" value="Aanmaken"></td>
            <td><input type="button" value="Terug" onclick="javascript:history.back();" /></td>
        </tr>
</table>
</form>
    <?php 
} 
else {
    
    // Insert the basic data of the new building
    $theDate = date('Y-m-d', strtotime($_POST['bouwjaar']."-1-1"));
    InsertBuilding($_POST['straat'], $_POST['huisnummer'], $_POST['postcode'], $theDate, $_POST['type']);
    
    // Get the id of the building that has just been added
    $id = GetLastBuilding();
    
    // Add the building details for all the specs that have an entered value
    $BuildingSpecs = GetAllBuildingSpecs();
    while($buildingspecRow = mysql_fetch_array($BuildingSpecs))
    {
        if (isset($_POST[$buildingspecRow['Id']]) && $_POST[$buildingspecRow['Id']] != "")
        {
            InsertBuildingDetail($id, $buildingspecRow['Id'], $_POST[$buildingspecRow['Id']]);
        }
    }

    echo "Het gebouw is toegevoegd<br>";
    echo "Klik <a href=\"gebouw.php\">hier</a> om terug te gaan naar het overzicht.";

}
?>
</section>

<?php
    include($_SERVER['DOCUMENT_ROOT']."/ObstacleWebApp/view/footer.php");
?>